﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Phoenix.Common.Helper
{
	public class RandomHelper
	{
		#region 产生一个20位唯一ID
		static string tmpstr = null;
		/// <summary>
		/// 产生20位的唯一ID
		/// </summary>
		/// <returns>返回随机字符串</returns>
		public static string GetSystemID()
		{
			return GetSystemID20();
		}
		/// <summary>
		/// 产生20位的唯一ID
		/// </summary>
		/// <returns>返回随机字符串</returns>
		public static string GetSystemID20()
		{
			string strFileName = "";

			strFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
			while (tmpstr == strFileName)
				strFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
			tmpstr = strFileName;

			strFileName += GetRandomStr(3);

			return strFileName;
		}
		/// <summary>
		/// 产生指定长度的唯一ID
		/// </summary>
		/// <param name="Length">长度</param>
		/// <returns>返回随机字符串</returns>
		public static string GetSystemID(int Length)
		{
			string strFileName = "";

			strFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
			while (tmpstr == strFileName)
				strFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
			tmpstr = strFileName;

			if (Length > 17)
				strFileName += GetRandomStr(Length - 17);
			else if (Length < 17)
				strFileName = GetRandomStr(Length);

			return strFileName;
		}
		#endregion

		#region 产生一个36位唯一Guid
		/// <summary>
		/// 产生一个36位唯一Guid
		/// </summary>
		/// <returns>返回随机字符串</returns>
		public static string GetGuid()
		{
            System.Guid guid = new Guid(RandomHelper.GetSystemID(32));
			string strGuid = guid.ToString();
			return strGuid;
		}
		#endregion

		#region 产生一个唯一的文件名
		/// <summary>
		/// 产生的一个20位唯一的文件名
		/// </summary>
		/// <param name="suffix">后缀名</param>
		/// <returns>返回随机字符串</returns>
		public static string GetDistinctFileName(string suffix)
		{
			string strFileName = "";
			//年
			strFileName += DateTime.Now.Year.ToString().Trim();
			//月
			strFileName += DateTime.Now.Month.ToString("00").Trim();
			//日
			strFileName += DateTime.Now.Day.ToString("00").Trim();
			//时
			strFileName += DateTime.Now.Hour.ToString("00").Trim();
			//分
			strFileName += DateTime.Now.Minute.ToString("00").Trim();
			//秒
			strFileName += DateTime.Now.Second.ToString("00");
			//加上4位随即数
			strFileName += GetRandomStr(6);

			//加上后缀
			if (suffix != "")
			{
				strFileName += "." + suffix;
			}

			return strFileName;

		}
		/// <summary>
		/// 产生的一个23位唯一的文件名
		/// </summary>
		/// <param name="suffix">后缀名</param>
		/// <returns>返回随机字符串</returns>
		public static string GetPhotoFileName(string suffix)
		{
			string strFileName = null;

			strFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
			strFileName += GetRandomStr(6);
			//加上后缀
			if (suffix != "")
			{
				strFileName += suffix;
			}

			return strFileName;
		}

		#endregion

		#region  获得随机字符 GetRandomStr
		/// <summary>
		/// 获取一个指定长度的随机字符串
		/// </summary>
		/// <param name="length">长度</param>
		/// <returns>返回随机字符串</returns>
		public static string GetRandomStr(int length)
		{
			return GetRandomStr(new Random(), length);
		}

		/// <summary>
		/// 产生一个随机指定长度的字符串
		/// </summary>
		/// <param name="dictionary">要产生随机的字符串</param>
		/// <param name="length">长度</param>
		/// <returns>返回随机字符串</returns>
		public static string GetRandomStr(string dictionary, int length)
		{
			return GetRandomStr(new Random(), dictionary, length);
		}

		/// <summary>
		/// 产生一个0-9组成的指定长度的随机字符串
		/// </summary>
		/// <param name="r">随机函数对象</param>
		/// <param name="length">长度</param>
		/// <returns>返回随机字符串</returns>
		public static string GetRandomStr(Random r, int length)
		{
			return GetRandomStr(r, "0123456789", length);
		}

		/// <summary>
		/// 产生随机字符串函数
		/// </summary>
		/// <param name="r">随机函数对象</param>
		/// <param name="dictionary">要产生随机的字符串</param>
		/// <param name="length">长度</param>
		/// <returns>返回随机字符串</returns>
		public static string GetRandomStr(Random r, string dictionary, int length)
		{
			if (r == null)
			{
				r = new Random();
			}

			StringBuilder sb = new StringBuilder();
			char[] chars = dictionary.ToCharArray();

			for (int i = 0; i < length; i++)
			{
				sb.Append(chars[r.Next(0, chars.Length)]);
			}

			return sb.ToString();
		}
		#endregion

		#region 利用ascii码，获取数字或字母组合的不重复随机字符串
		/// <summary>
		/// 利用ascii码，获取数字或字母组合的不重复随机字符串
		/// </summary>
		/// <param name="maxLength"></param>
		/// <returns></returns>
		public static string GetRandomString(int maxLength)
		{
			string randomString = null;
			Random rNumber = new Random(Environment.TickCount);
			for (int i = 0; i < maxLength; i++)
			{
				randomString = randomString + Convert.ToChar(rNumber.Next(48, 57));//number  65, 90
			}
			return randomString;
		}
		#endregion

		#region 获得随机整形数
		/// <summary>
		/// 获得随机整形数
		/// </summary>
		/// <param name="i">随机值</param>
		/// <param name="length">长度</param>
		/// <param name="up">随机数的上限</param>
		/// <param name="down">随机数的下限</param>
		/// <returns></returns>
		public static int GetRandomNum(int i, int length, int up, int down)
		{
			int iFirst = 0;
			Random ro = new Random(i * length * unchecked((int)DateTime.Now.Ticks));
			iFirst = ro.Next(up, down);
			return iFirst;
		}

		/// <summary>
		/// 获得8位随机整形数
		/// </summary>
		/// <returns></returns>
		public static int GetRandom()
		{
			Random Rd = new Random();
			return Rd.Next(10000000, 99999999);
		}

		/// <summary>
		/// 获得随机整形数
		/// </summary>
		/// <param name="up">随机数的上限</param>
		/// <param name="down">随机数的下限</param>
		/// <returns></returns>
		public static int GetRandomNum(int up, int down)
		{
			Random Rd = new Random();
			return Rd.Next(up, down);
		}
		#endregion

		#region 返回不重复随机数数组
		/// <summary>
		/// 返回不重复随机数数组
		/// </summary>
		/// <param name="Number">随机数个数</param>
		/// <param name="minNum">随机数下限</param>
		/// <param name="maxNum">随机数上限</param>
		/// <returns>返回不重复随机数数组</returns>
		public static int[] GetRandomArray(int Number, int minNum, int maxNum)
		{
			int j;
			int[] b = new int[Number];
			Random r = new Random();
			for (j = 0; j < Number; j++)
			{
				int i = r.Next(minNum, maxNum);
				int num = 0;
				for (int k = 0; k < j; k++)
				{
					if (b[k] == i)
					{
						num = num + 1;
					}
				}
				if (num == 0)
				{
					b[j] = i;
				}
				else
				{
					j = j - 1;
				}
			}
			return b;
		}
		#endregion

		#region 获取随机字符串
		/// <summary>
		/// 获取随机字符串
		/// </summary>
		/// <param name="strLength">字符串长度</param>
		/// <param name="Seed">随机函数种子值</param>
		/// <returns>指定长度的随机字符串</returns>
		public static string RndString(int strLength, params int[] Seed)
		{
			string strSep = ",";
			char[] chrSep = strSep.ToCharArray();

			//因1与l不容易分清楚，所以去除
			//string strChar = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z"
			// + ",A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";
			string strChar = "0,2,3,4,5,6,7,8,9"
			 + ",A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";

			string[] aryChar = strChar.Split(chrSep, strChar.Length);

			string strRandom = string.Empty;
			Random Rnd;
			if (Seed != null && Seed.Length > 0)
			{
				Rnd = new Random(Seed[0]);
			}
			else
			{
				Rnd = new Random();
			}

			//生成随机字符串
			for (int i = 0; i < strLength; i++)
			{
				strRandom += aryChar[Rnd.Next(aryChar.Length)];
			}

			return strRandom;
		}

		private static char[] constant =   
          {   
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
          };

		/// <summary>
		/// 获取随机字符串
		/// </summary>
		/// <param name="Length">长度</param>
		/// <returns>随机字符串</returns>
		public static string GenerateRandom(int Length)
		{

			System.Text.StringBuilder newRandom = new System.Text.StringBuilder(52);
			Random rd = new Random();
			for (int i = 0; i < Length; i++)
			{
				newRandom.Append(constant[rd.Next(52)]);
			}
			return newRandom.ToString();
		}
		#endregion

		#region 随机密码生成
		/// <summary>
		/// 随机密码生成
		/// </summary>
		/// <param name="pwdlen">密码程度</param>
		/// <returns>随机密码</returns>
		public static string MakePassword(int pwdlen)
		{
			string randomchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			string tmpstr = "";
			int iRandNum;
			Random rnd = new Random();
			for (int i = 0; i < pwdlen; i++)
			{
				iRandNum = rnd.Next(randomchars.Length);
				tmpstr += randomchars[iRandNum];
			}
			return tmpstr;
		}
		#endregion

		/// <summary>
		/// 生成不重复的随机数
		/// </summary>
		/// <param name="N"></param>
		/// <returns></returns>
		public static string RandCode(int N)
		{
			char[] arrChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
			StringBuilder num = new StringBuilder();
			Random rnd = new Random(Guid.NewGuid().GetHashCode());
			for (int i = 0; i < N; i++)
			{
				num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
			}
			return num.ToString();
		}
 

	}
}
